﻿/*
 * Contributor: Gary Zhang
 * Email: cbcye@live.com
 * Web Site: http://cbcye.cnblogs.com | http://www.cbcye.com 
 * Code reference: http://www.imobilebbs.com
 * */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using Microsoft.BizTalk.Message.Interop;
using Microsoft.BizTalk.Component.Interop;

namespace BizTalkWCFOperationPromoteComponent
{
    [ComponentCategory(CategoryTypes.CATID_PipelineComponent)]
    [ComponentCategory(CategoryTypes.CATID_Encoder)]
    [System.Runtime.InteropServices.Guid("b9991b36-1e63-490b-98b4-672e1d7315b1")]
    public class BizTalkWCFOperationPromoter : IBaseComponent, IComponentUI, IComponent
    {
        /// <summary>
        /// Configure basic properties
        /// </summary>
        #region IBaseComponent
        public string Description
        {
            get { return "Promote message's operation "; }
        }

        public string Name
        {
            get { return "WCFOperationPromoteEncoder"; }
        }

        public string Version
        {
            get { return "1.0"; }
        }
        #endregion

        /// <summary>
        /// Configure component UI properties
        /// </summary>
        #region IComponentUI
        public IntPtr Icon
        {
            get { return new IntPtr(); }
        }

        public System.Collections.IEnumerator Validate(object projectSystem)
        {
            return null;
        }
        #endregion

        /// <summary>
        /// Process the income message, promote message's context.
        /// </summary>
        /// <param name="pContext">context of the income message</param>
        /// <param name="pInMsg">income message</param>
        /// <returns>processed message</returns>
        #region IComponent
        public IBaseMessage Execute(IPipelineContext pContext, IBaseMessage pInMsg)
        {
            //Get the data stream
            Stream originalStream = pInMsg.BodyPart.GetOriginalDataStream();
            //Get the message type(root name).
            string messageType = ReadMessageType(originalStream);

            originalStream.Seek(0, SeekOrigin.Begin);
            if (messageType != null)
            {
                try
                {
                    //Promote the message type to the context
                    pInMsg.Context.Promote("Operation", "http://schemas.microsoft.com/BizTalk/2003/system-properties", messageType);
                }
                catch (Exception ex)
                {
                    throw ex;
                }

            }
            return pInMsg;

        }

        /// <summary>
        /// Read the first element's local name of the message
        /// </summary>
        /// <param name="messageStream"></param>
        /// <returns></returns>
        public static string ReadMessageType(Stream messageStream)
        {
            if (messageStream == null)
            {
                throw new ArgumentNullException("messageStream");
            }
            XmlReader xmlReader = new XmlTextReader(messageStream);
            xmlReader.Read();
            if (xmlReader.IsStartElement())
            {
                return xmlReader.LocalName;
            }
            return string.Empty;
        }
        #endregion
    }
}
